<?php

namespace app\command;

use app\admin\model\Config;
use app\Constant\ApiConstant;
use app\model\DistributionSetting;
use app\model\Live;
use app\model\Talent;
use app\service\DeepSeekService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use util\Logger;
use Wechat\CommerceApp;

class LiveNoticeTaskCommand extends Command
{
    protected function configure()
    {
        $this->setName('live-notice')
            ->setDescription('获取达人预约直播列表');
    }

    protected function execute(Input $input, Output $output)
    {
        $mini_program_appid = Config::where('name', 'wx_xcx_appid')->value('value');
        $talent_list        = Talent::column('talent_appid');
        $appids             = [];
        $share_text         = '🔥【爆款直播间】火热预约中！\r\n🌟精选好货·全网热销爆品\r\n🎁预约享专属福利·开播惊喜不断\r\n🛒扫码立即预约👉\r\n⏰准时开播·福利抢先拿\r\n💯正品保障·售后无忧\r\n📢前100名预约送额外好礼\r\n✨主播在线等你·互动答疑';
        foreach ($talent_list as $talent_appid) {
            try {
                $save             = [];
                $sharer_info_list = CommerceApp::getInstance()->getLiveNoticeList($talent_appid, $mini_program_appid);
                if ($sharer_info_list) {
                    $appids[] = $talent_appid;
                    $db_live  = Live::where('appid', $talent_appid)->where('live_status', 0)->column('appid, id, start_time');
                    foreach ($sharer_info_list as $item) {
                        if (!isset($db_live[$talent_appid])) {
                            $save[] = [
                                'type'                => 1,
                                'appid'               => $talent_appid,
                                'export_id'           => $item['notice_id'],
                                'description'         => $item['description'],
                                'start_time'          => $item['start_time'],
                                'live_status'         => 0,
                                'promoter_share_link' => '',
                                'share_text'          => $share_text,
                                'status'              => 1,
                            ];
                        } else {
                            $save[] = [
                                'id'                  => $db_live[$talent_appid]['id'],
                                'export_id'           => $item['notice_id'],
                                'description'         => $item['description'],
                                'start_time'          => $item['start_time'],
                                'live_status'         => 0,
                                'promoter_share_link' => '',
                                'status'              => 1,
                            ];
                        }
                    }
                    if ($save) {
                        (new Live())->saveAll($save);
                    }
                } else {
                    Live::where('appid', $talent_appid)->update(['status' => 0]);
                }
            } catch (\Exception $e) {
//                $log = [
//                    'appid' => $talent_appid,
//                    'error' => $e->getMessage(),
//                    'file'  => $e->getFile(),
//                    'line'  => $e->getLine(),
//                ];
//                Logger::error('预约直播列表获取失败', $log);
                continue;
            }
        }
//        //删除过期的直播
//        if ($appids) {
//            Live::where('appid', 'not in', $appids)->where('live_status', 0)->delete();
//        }

    }
}
